package kafeisc.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @Classname ${NAME}
 * @Description TODO
 * @Date 2020/4/27 17:23
 * @Created 
 */
@WebFilter(filterName = "EncodingFilter")
public class EncodingFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        // 处理响应中文的乱码问题
        resp.setContentType("text/html;charset=utf-8");
        // 处理请求中文的乱码问题  post/get
        HttpServletRequest req1 = (HttpServletRequest)req;
        HttpServletResponse resp1 = (HttpServletResponse)resp;
        HttpServletRequest myRequest = new MyRequest(req1);
        chain.doFilter(myRequest,resp1);
    }

    public void init(FilterConfig Config) throws ServletException {

    }

}

class MyRequest extends HttpServletRequestWrapper{

    // 1. 引入增强的 request 对象
    private HttpServletRequest request;
    // 2. 定义参数目的：处理 get请求中的中文乱码一次
    boolean hasEncode;

    public MyRequest(HttpServletRequest req) {
        super(req);
        this.request = req;
    }
    // 获取请求参数的方法，都需要处理中文乱码问题

    @Override
    public Map<String, String[]> getParameterMap() {
        // 获取请求参数
        String method = request.getMethod();
        if(method.equalsIgnoreCase("post")){
            try {
                request.setCharacterEncoding("utf-8");
                return super.getParameterMap();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return request.getParameterMap();
        }else if(method.equalsIgnoreCase("get")){
            Map<String,String[]> parameterMap = request.getParameterMap();
            // 遍历map集合，获取里面的每一个参数，进行中文乱码问题处理，处理来一次
            if(!hasEncode){
                for(String parameterName:parameterMap.keySet()){
                    String[] values = parameterMap.get(parameterName);
                    if (values != null){
                        for (int i=0;i<values.length;i++){
                            String value = values[i];
                            try {
                                value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    hasEncode = true;
                }
                return parameterMap;
            }
        }
        return super.getParameterMap();
    }

    @Override
    public String getParameter(String name) {
        Map<String,String[]> parameterMap = request.getParameterMap();
        String[] values = parameterMap.get(name);
        if (values == null){
            return null;
        }
        return values[0];
    }

    @Override
    public String[] getParameterValues(String name) { // 获取复选框的值
        Map<String,String[]> parameterMap = request.getParameterMap();
        String[] values = parameterMap.get(name);
        return values;
    }
}
